package com.css.fxfzaqbz.modules.data.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess2.entity.KrTask;
import com.css.fxfzaqbz.modules.data.entity.BigDisposeManager;
import com.css.fxfzaqbz.modules.data.repository.BigDisposeManagerRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class BigDisposeManagerRepositoryImpl implements BigDisposeManagerRepository {
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Override
    public void saveTask(BigDisposeManager bigDisposeManager1) {
        String sql="insert into sys_big_dispose_manager (id,ia_id,ia_opinion,ia_status,task_name,del_flag,create_user,create_time) values " +
                "(:id,:iaId,:iaOpinion,:iaStatus,:taskName,:delFlag,:createUser,:createTime)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(bigDisposeManager1);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
    }

    @Override
    public BigDisposeManager getTask(String iaId, String a) {
        try {
            String sql="select * from sys_big_dispose_manager where ia_id='"+iaId+"' and del_flag='"+ YNEnum.N+"' and task_name='"+a+"'";
            BigDisposeManager bigDisposeManager = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(BigDisposeManager.class));
            return bigDisposeManager;
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public List<BigDisposeManager> getList(String iaId) {
        String sql="select * from sys_big_dispose_manager where ia_id='"+iaId+"' and del_flag='"+ YNEnum.N+"'";
        List<BigDisposeManager> bigDisposeManagers = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(BigDisposeManager.class));
        return bigDisposeManagers;
    }

    @Override
    public void upStatus(String id) {
        String sql = "update sys_big_dispose_manager set" +
                " ia_status = '"+2+"' " +
                " where ia_id = '" + id + "'";
        jdbcTemplate.update(sql);

    }
}
